//
//  ZXPopController.m
//  ZXOCWeiBo
//
//  Created by mac on 15/11/30.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXPopController.h"

@interface ZXPopController()
/**
 *  PopView里面要显示的内容
 */
@property(nonatomic,strong)UIView* contentView;

/**
 *  要显示内容后面的图片
 */
@property(nonatomic,strong)UIImageView* popView;

/**
 *  遮盖
 */
@property(nonatomic,strong)UIButton * conver;
@end

@implementation ZXPopController


/**
 *  返回一个实例
 *
 *  @param contentView contentView Pop里面内容VIew
 */
-(instancetype)initWithContenView:(UIView *)contentView
{
    if (self = [super init])
    {
        self.contentView = contentView;
        
        //创建popView
        self.popView = [[UIImageView alloc]init];
        //设置图片的交互
        self.popView.userInteractionEnabled = YES;
        
        CGFloat leftMargin = 10;
        CGFloat rightMargin = 10;
        CGFloat topMargin = 15;
        CGFloat bottomMargin = 10;
        CGFloat popW = self.contentView.w + leftMargin + rightMargin;
        CGFloat popH = self.contentView.h + topMargin + bottomMargin;
        self.popView.size = CGSizeMake(popW, popH);
        
        //设置contentView x，y
        self.contentView.x = leftMargin;
        self.contentView.y = topMargin;
        
        //吧contentView添加到popVIew中
        [self.popView addSubview:contentView];
        
        //初始化遮盖
        self.conver = [[UIButton alloc]init];
        self.conver.bounds = [UIScreen mainScreen].bounds;
        [self.conver addTarget:self action:@selector(converClick) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    return  self;
}

/**
 *  监听遮盖视图的点击
 */
-(void)converClick
{
   
    //移除popView
    [self.popView removeFromSuperview];
    
    //移除遮盖视图
    [self.conver removeFromSuperview];
}

/**
 *  在那个View显示 将View添加到inView的父控件上面
 */
-(void)showInView:(UIView *)inView
{
    
    [self showInView:inView position:ZXPopViewPosltionBottomCenter];

}

-(void)showInView:(UIView *)inView position:(ZXPopViewPosition)positon
{
    //0.显示遮盖
    self.conver.x = 0;
    self.conver.y = 0;
    //获取窗口
    UIWindow * keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:self.conver];
    
    
    //在导航条上面添加遮盖是不可取的，因为超出的父控件的部分可以显示但是不能接收事件，并且遮盖视图会阻挡着popView的点击。（如果给popView设置为Btn的时候）.所以要添加到window上面
    //1.显示popView
    //设置中心点
    //toVIew如果写nil 代表转换成窗口坐标准
    CGPoint inViewCenterInWidow = [[inView superview] convertPoint:inView.center toView:nil];
    
    inViewCenterInWidow.y += self.popView.h * 0.5 + inView.h * 0.5;
    
    
    //添加到window上
    [keyWindow addSubview:self.popView];
    //    NSLog(@"%@",[inView superview]);
    //添加到导航栏上
    //    [[inView superview] addSubview:self.popView];
    
    if (positon == ZXPopViewPosltionBottomleft)
    {
        //换图片
        self.popView.image = [UIImage resizeImageWithName:@"popover_background_left"];
        inViewCenterInWidow.x += (self.popView.w - inView.w) * 0.5;
        
    }
    else if (positon == ZXPopViewPosltionBottomRight)
    {
        //换图片
        self.popView.image = [UIImage resizeImageWithName:@"popover_background_right"];
        inViewCenterInWidow.x -= (self.popView.w - inView.w) * 0.5;
    }
    else
    {
        //换图片
        self.popView.image = [UIImage resizeImageWithName:@"popover_background"];
    }
    self.popView.center = inViewCenterInWidow;
}

/**
 *  隐藏当前的popView
 */
-(void)dissmiss
{
    [self converClick];
}

//设置遮盖透明度
-(void)setAlpha:(CGFloat)alpha
{
    if (alpha > 0)
    {
        //设置遮盖视图的背景
        self.conver.backgroundColor = [UIColor blackColor];
        self.conver.alpha = alpha;
    }
    
}
@end
